package org.example.consumer.pull;

import org.apache.rocketmq.client.consumer.DefaultLitePullConsumer;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.protocol.heartbeat.MessageModel;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * @author zhangjg
 * @desc c
 * @date 2023/04/03 09:47
 **/
public class C1 {

    public static volatile boolean running = true;


    public static void main(String[] args) throws Exception {

        ExecutorService executorService = Executors.newCachedThreadPool();
        int cnt = 20;
        for (int i = 1; i <= cnt; i++) {
            executorService.submit(new Runnable() {
                @Override
                public void run() {
                    DefaultLitePullConsumer litePullConsumer = null;
                    try {
                        litePullConsumer = new DefaultLitePullConsumer("lite_pull_consumer_test");
                        litePullConsumer.setNamesrvAddr("127.0.0.1:9876");
                        litePullConsumer.subscribe("t1", "*");
                        litePullConsumer.setPullBatchSize(20);
                        litePullConsumer.setMessageModel(MessageModel.CLUSTERING);
                        litePullConsumer.start();
                        System.out.println(Thread.currentThread().getName()+" consumer started");
                        while (running) {
                            List<MessageExt> messageExts = litePullConsumer.poll();
                            System.out.printf(Thread.currentThread().getName() + "  |%s%n", messageExts);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        litePullConsumer.shutdown();
                    }
                }
            });

        }

        TimeUnit.SECONDS.sleep(120);
        executorService.shutdown();
    }


}
